package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.SelectAdapter;
import com.lanlian.commonlib.bean.RegisterEnumInfo;

import java.util.ArrayList;

/**
 * 多选/单选对话框
 */
public class SelectSheetDialog extends Dialog implements View.OnClickListener {

    private TextView mTvTitle;
    private TextView mTvFinish;
    private ListView mListView;
    private TextView mTvCancel;

    private int mChoiceMode;
    private String mTitle;
    private Context mContext;

    private ArrayList<RegisterEnumInfo> mItems = new ArrayList<>();
    private OnFinishClickListener mOnFinishClickListener;
    private ArrayList<Integer> positions = new ArrayList<>();
    private int position = -1;

    public SelectSheetDialog(Context context, String title, int choiceMode, ArrayList<RegisterEnumInfo> itmes) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mTitle = title;
        this.mItems = itmes;
        this.mChoiceMode = choiceMode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_multi_select_sheet);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        initView();
        setListeners();
        mTvTitle.setText(mTitle);
        initAdapter();
        if (mChoiceMode == AbsListView.CHOICE_MODE_SINGLE) {
            mListView.setItemChecked(position, true);
        } else {
            for (Integer position : positions) {
                mListView.setItemChecked(position, true);
            }
        }
        if (mListView.getAdapter().getCount() > 5) {
            resetListViewHeight(mListView);
        }
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mListView.clearChoices();
                if (mChoiceMode == AbsListView.CHOICE_MODE_SINGLE) {
                    mListView.setItemChecked(position, true);
                } else {
                    for (Integer position : positions) {
                        mListView.setItemChecked(position, true);
                    }
                }
            }
        });
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mTvFinish = findViewById(R.id.tv_finish);
        mListView = findViewById(R.id.list_view);
        mTvCancel = findViewById(R.id.tv_cancel);
    }

    private void setListeners() {
        mTvFinish.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    private void resetListViewHeight(ListView lv) {
        ListAdapter listAdapter = lv.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < 5; i++) {
            View listItem = listAdapter.getView(i, null, lv);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = totalHeight + (lv.getDividerHeight() * (listAdapter.getCount() - 1));
        lv.setLayoutParams(params);
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(com.lanlian.uikit.R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

    private void initAdapter() {
        mListView.setChoiceMode(mChoiceMode);
        mListView.setAdapter(new SelectAdapter(mContext, mItems, mChoiceMode));
    }

    public void setOnFinishClickListener(OnFinishClickListener onFinishClickListener) {
        this.mOnFinishClickListener = onFinishClickListener;
    }

    public interface OnFinishClickListener {
        void onFinishClick(ArrayList<RegisterEnumInfo> itmes);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancel) {
            dismiss();
        } else if (v.getId() == R.id.tv_finish) {
            ArrayList<RegisterEnumInfo> checked = new ArrayList<>();
            if (mChoiceMode == AbsListView.CHOICE_MODE_MULTIPLE) {
                ArrayList<Integer> ids1 = new ArrayList<>();
                SparseBooleanArray sparseBooleanArray = mListView.getCheckedItemPositions();
                for (RegisterEnumInfo mItem : mItems) {
                    if (sparseBooleanArray.get(mItems.indexOf(mItem))) {
                        checked.add(mItem);
                        ids1.add(mItems.indexOf(mItem));
                    }
                }
                if (checked.size() == 0) {
                    Toast.makeText(mContext, "请选择选项", Toast.LENGTH_SHORT).show();
                    return;
                }
                positions = ids1;
            } else if (mChoiceMode == AbsListView.CHOICE_MODE_SINGLE) {
                if (mListView.getCheckedItemPosition() == -1) {
                    Toast.makeText(mContext, "请选择选项", Toast.LENGTH_SHORT).show();
                    return;
                }
                checked.add(mItems.get(mListView.getCheckedItemPosition()));
                position = mListView.getCheckedItemPosition();
            }
            mOnFinishClickListener.onFinishClick(checked);
            dismiss();
        }
    }

    public void setMultiChecked(ArrayList<Integer> positions) {
        this.positions = positions;
    }

    public void setSingleChecked(int position) {
        this.position = position;
    }

}
